/** 
 * Description:首页广告维护
 * Date:2013-1-31
 * Author:Along	
*/
package com.community.shopping.web;

import java.io.File;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.community.shopping.domain.Goods;
import com.community.shopping.domain.GoodsType;
import com.community.shopping.domain.IndexAdv;
import com.community.shopping.service.logic.IndexAdvManager;
import com.community.shopping.util.FileUpload;
import com.community.shopping.util.FileUtil;
import com.community.shopping.util.RequestHelp;

@Controller 
public class IndexAdvControl {
	@Autowired  
    private IndexAdvManager indexAdvManager;
	
	/**
	 * 广告列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/advlist")
	 public ModelAndView getlist(HttpServletRequest request) 
   { 
     ModelAndView modelAndView = new ModelAndView("index/adv_list.jsp"); 
     Integer shopId=RequestHelp.getShopId(request);
     List advs=indexAdvManager.getIndexAdvs(shopId);
	 modelAndView.addObject("advs", advs); 	 
     return modelAndView; 		
   } 
	
	/**
	 * 新增广告
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/advadd")
	 public ModelAndView addtype(HttpServletRequest request) 
    { 
      ModelAndView modelAndView = new ModelAndView("index/adv_add.jsp"); 
      return modelAndView; 		
    } 
	/**
	 * 修改广告
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/advedit")
	 public ModelAndView editadv(HttpServletRequest request) 
    { 
      ModelAndView modelAndView = new ModelAndView("index/adv_edit.jsp"); 
	  String id=request.getParameter("id");
	  IndexAdv adv=indexAdvManager.getIndexAdv(Integer.parseInt(id));
	  modelAndView.addObject("adv", adv); 
     return modelAndView; 		
	 } 
	/**
	 * 保存广告
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/advsave")
	 public ModelAndView save(HttpServletRequest request,GoodsType goodsType) throws Exception 
    { 
      ModelAndView modelAndView = new ModelAndView("advlist.do");    
      String advId=request.getParameter("advId");
      Map parameter=request.getParameterMap();
 	  IndexAdv adv=new IndexAdv();	
 	 if(StringUtils.isBlank(advId))
 	 {	
 		 BeanUtils.populate(adv, parameter);
 		 Integer shopId=RequestHelp.getShopId(request);
 		 adv.setShopId(shopId);
 		 adv.setStatus(1);		
 	 }else
 	 {
 		 adv=indexAdvManager.getIndexAdv(Integer.parseInt(advId));
 		 BeanUtils.populate(adv, request.getParameterMap());
 	 }
		  
 	indexAdvManager.save(adv);
    return modelAndView; 		
    } 
	/**
	 * 删除广告
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/advdel")
	 public ModelAndView deladv(HttpServletRequest request) 
    { 
      ModelAndView modelAndView = new ModelAndView("advlist.do"); 
	  String id=request.getParameter("id");
	  IndexAdv adv=indexAdvManager.getIndexAdv(Integer.parseInt(id));
	  indexAdvManager.delete(adv);
     return modelAndView; 		
	 } 
	/**
	 * 上传广告图片
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/advUpload")
	 public void advUpload(HttpServletRequest request,HttpServletResponse response) throws Exception
	    { 
		  String systemBase=request.getSession().getServletContext().getRealPath("/");
		  String folder=systemBase+"/data/"+RequestHelp.getShopId(request);
		  FileUtil.createPath(folder, "index,adv");
		   
	      String serverFileName=FileUpload.fileUpload(request,"Filedata",folder+"/index/adv/");
	      response.getWriter().print("/data/"+RequestHelp.getShopId(request)+"/index/adv/"+serverFileName); 		
		  		
	 } 


}
